<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '规格添加' : '规格修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      user:{{ user }}
      <div>attrsArr:{{ attrsArr }}</div>
      <el-form :model="user">
        <el-form-item label="规格名称" label-width="120px">
          <el-input v-model="user.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="规格属性"
          label-width="120px"
          v-for="(item, index) in attrsArr"
          :key="index"
        >
          <div class="inputAndBtn">
            <el-input v-model="item.val" autocomplete="off"></el-input>
            <el-button
              type="primary"
              v-if="index == 0"
              @click="attrsArr.push({ val: '' })"
              >新增规格属性</el-button
            >
            <el-button type="danger" v-else @click="attrsArr.splice(index, 1)"
              >删除</el-button
            >
          </div>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  reqspecsadd,
  reqspecsedit,
  reqspecsinfo,
} from "../../../http/api";
import { formClose } from "../../../mixins";
import { errorAlert, successAlert } from "../../../utils/alert";
export default {
  // 4.接收info
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      // 5.1 初始化表单数据
      user: {
        specsname: "",
        attrs: "",
        status: 1,
      },
      attrsArr: [
        {
          val: "",
        },
        {
          val: "",
        },
      ],
    };
  },
  computed: {
    ...mapGetters({}),
  },
  mounted() {},
  methods: {
    ...mapActions({
      reqArr: "specs/reqArr",
    }),

    // 清空user
    empty() {
      this.user = {
        specsname: "",
        attrs: "",
        status: 1,
      };
      this.attrsArr = [
        {
          val: "",
        },
        {
          val: "",
        },
      ];
    },
    checkData() {
      return new Promise((resolve, reject) => {
        if (this.user.specsname == "") {
          errorAlert("规格名称不能为空");
          return;
        }
        if (this.attrsArr.some((item) => item.val === "")) {
          errorAlert("规格属性不能为空");
          return;
        }
        // attrsArr:[ { "val": "2" }, { "val": "2" } ,, { "val": "3" }]-->[2,2,3]-->[2,3]
        // attrsArr:[ { "val": "2" }, { "val": "4" } ,, { "val": "3" }]-->[2,4,3]-->[2,4,3]
        if (
          new Set(this.attrsArr.map((item) => item.val)).size <
          this.attrsArr.length
        ) {
          errorAlert("规格属性不能重复");
          return;
        }
        resolve();
      });
    },
    // 5.4 点了添加
    add() {
      this.checkData().then(() => {
        //处理attrs
        this.user.attrs = this.attrsArr.map((item) => item.val).join(",");

        reqspecsadd(this.user).then((res) => {
          if (res.data.code == 200) {
            // ①弹成功
            successAlert(res.data.msg);
            // ②弹框内容清空
            this.empty();
            // ③弹框消失
            this.info.isshow = false;
            // ④重新请求列表
            this.reqArr();
          }
        });
      });
    },

    // 8. 获取一条数据
    getOne(id) {
      reqspecsinfo({ id: id }).then((res) => {
        if (res.data.code === 200) {
          this.user = res.data.list[0];
          //  this.user.attrs  ['S','M','L']
          // attrsArr = [{val:'S'},{val:'M'},{val:'L'}]
          this.attrsArr = this.user.attrs.map((item) => ({ val: item }));
        }
      });
    },
    // 9.修改
    update() {
      this.checkData().then(() => {
        this.user.attrs = this.attrsArr.map((item) => item.val).join(",");
        reqspecsedit(this.user).then((res) => {
          if (res.data.code == 200) {
            // ①弹成功
            successAlert(res.data.msg);

            // ②弹框内容清空
            this.empty();
            // ③弹框消失
            this.info.isshow = false;
            // ④重新请求列表
            this.reqArr();
          }
        });
      });
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.inputAndBtn {
  display: flex;
  .el-button {
    width: auto;
  }
  .el-input {
    flex: 1;
  }
}
</style>